Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[legacy-framework] allow useQuery to run during SSR or SSG #1940

Merged
merged 6 commits into from
May 10, 2021

Conversation

jxe
Copy link

@jxe jxe commented Feb 15, 2021

This allows useQuery to run on the server, iff there's precached data. We do this by calling useReactQuery with enabled:false and seeing if there's data returned. If there's no data, we throw a suspender to emulate the previous behavior.

To prefetch data, you can run something like this in getStaticProps

import { QueryCache } from "react-query"
const cache = new QueryCache()
const queryKey = getQueryKey(queryFn, queryArgs)
await cache.prefetchQuery(queryKey, () => queryFn(queryArgs))

and then you can pass the resulting cache to the client with

import { dehydrate } from "react-query/hydration"
return { props: { queryCache: dehydrate(cache) } }

So you get identical results on first render.

@jxe jxe force-pushed the server-side-useQuery branch from b7c15ed to 6c4b05e Compare February 15, 2021 20:33
@MrLeebo MrLeebo requested a review from flybayer as a code owner May 2, 2021 16:06
@MrLeebo
Copy link
Member

MrLeebo commented May 2, 2021

@jxe @flybayer I resolved the merge conflicts and added an exclusion so useQuery doesn't run during SSG when in legacyMode. The failing test passed on my local environment though...

@MrLeebo
Copy link
Member

MrLeebo commented May 6, 2021

Switched to reactRoot and would you look at that, a green checkmark! It's what I was hoping for, but you can never be sure what you're going to get 😁

@flybayer
Copy link
Member

flybayer commented May 6, 2021

Ok last thing @MrLeebo, do we need to update or add any docs?

@MrLeebo
Copy link
Member

MrLeebo commented May 6, 2021

@flybayer Definitely. At minimum, the content at https://blitzjs.com/docs/pages#automatic-static-optimization will need to be updated, and there may be other broader repercussions I'm not even aware of yet.

@flybayer flybayer changed the title allow useQuery to run during SSG allow useQuery to run during SSR or SSG May 10, 2021
@flybayer flybayer merged commit d0a7e23 into blitz-js:canary May 10, 2021
@itsdillon itsdillon changed the title allow useQuery to run during SSR or SSG [legacy-framework] allow useQuery to run during SSR or SSG Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants